package com.bw.shop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.bw.common.constants.TokenConstants;
import com.bw.common.domain.Admin;
import com.bw.common.domain.Car;
import com.bw.common.domain.Logs;
import com.bw.common.result.Result;
import com.bw.common.utils.JwtUtils;
import com.bw.shop.mapper.CarMapper;
import com.bw.shop.service.CarService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author：HaoYunde
 * @Package：com.bw.shop.service.impl
 * @Project：hyd-boot
 * @name：CarServiceImpl
 * @Date：2024/6/17 17:11
 */
@Service
public class CarServiceImpl implements CarService {
    @Autowired
    private CarMapper carMapper;
    @Autowired
    public StringRedisTemplate redisTemplate;
    @Autowired
    public HttpServletRequest request;
    @Autowired
    public RabbitTemplate rabbitTemplate;

    @Override
    public List<Car> findCarList() {
        return carMapper.findCarList();
    }

    @Override
    public List<Logs> findLogsList() {
        return carMapper.findLogsList();
    }

    @Override
    public Result addLogs(Logs logs) {
        Integer i= carMapper.addLogs(logs);
        return Result.success();
    }

    @Override
    public Result updateAdminStatus(String price) {
        //获取当前登录人信息
        String header = request.getHeader(TokenConstants.TOKEN);
        String key = JwtUtils.getUserKey(header);
        System.out.println(key);
        String s = redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + header);
        Admin admin = JSONObject.parseObject(s, Admin.class);
        Integer i= carMapper.updateAdminStatus(price,admin.getAdminId());
        return Result.success();
    }

    @Override
    public Result updateStatus(Integer id) {
        Integer i=carMapper.updateStatus(id);
        if(i>0){
            rabbitTemplate.convertAndSend("cg");
        }
        return Result.success();
    }


}
